//
//  WBNavigationController.swift
//  TYCWeiBo
//
//  Created by 中发 on 2018/5/29.
//  Copyright © 2018年 中发. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //隐藏默认的navigationBar
        navigationBar.isHidden = true
    }
    //重写了push方法，所有的push都会调用这个方法
    //viewController是被push 的控制器，设置他的左侧按钮为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //如果不是栈底控制器就不需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            var title = "返回"
            //判断控制器的级数
            if childViewControllers.count == 1 {
                //如果级数只有1级控制器，则显示title
                title = childViewControllers.first?.title ?? "返回"
            }
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                //取出只定义的item
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(posPresent))
            }
        }
        super.pushViewController(viewController, animated: true)
    }

    //pop返回到上一层控制器
    @objc private func posPresent() {
        popViewController(animated: true)
    }
}
